// ==UserScript==
// @name         码云
// @namespace    https://gitee.com/cn-src/browser-plugin-support
// @version      0.1
// @description  码云批量同步 github 项目
// @author       cn-src
// @match        https://gitee.com/*
// @grant        none
// ==/UserScript==

(function () {
    'use strict';
    function getUrlParms(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        if (r != null)
            return unescape(r[2]);
        return null;
    }
    var $githubBtn = $('<a href="javascript:void(0);" id="sync_all_github_projects" class="item " title="同步Github">同步Github</a>')
    $githubBtn.click(function () {
        var projects = [
            'wechat-pay',
            'aliyun-oss',
            'aliyun-sms',
            'intellij-file-icon',
            'intellij-image-icon',
            'intellij-plugins',
            'platform-bom-extension',
            'poi-excel',
            'wechat-pay'
        ]
        for (var i = 0; i < projects.length; i++) {
            window.open('https://gitee.com/cn-src/' + projects[i] + '?autoopen=true')
        }
    })
    $("#my-gitee-dropdown").after($githubBtn)
    if (getUrlParms('autoopen')) {
        // 打开页面后自动同步 github 项目
        if (!sessionStorage.getItem("div.ui.small.button.orange.ok" + window.location)) {
            sessionStorage.setItem("div.ui.small.button.orange.ok" + window.location, true);
            $("div.ui.small.button.orange.ok").click()
        }
    }
})();